package at.ltasat.classifier.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import at.ltasat.classifier.ClassifierFactories;
import at.ltasat.classifier.ObjectFeaturesFactory;
import at.ltasat.classifier.ClazzManager;
import at.ltasat.classifier.Classifier;
import static at.ltasat.util.IO.*;

public final class ClassifierIO {

	public static <O, D> void write(final OutputStream out, final Classifier<O, D> classifier, final ClazzManagerIO<D> classesIO) throws IOException {
		classesIO.write(out, classifier.getClazzManager());
		writeString(out, classifier.getName());
		classifier.write(out);
	}
	
	public static <O, D> Classifier<O, D> read(final InputStream in, final ClassifierFactories<O, D> factories, final ClazzManagerIO<D> classesIO, ObjectFeaturesFactory<O> attributesFactory) throws IOException {
		ClazzManager<D> classes = classesIO.read(in);
		String name = readString(in);
		return factories.get(name).readClassifier(classes, attributesFactory, in);
	}

}
